/**
 * 
 * 
 */
package com.cjfather.redis.steam.aspect;

import java.lang.reflect.Method;

import javax.annotation.Resource;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import com.cjfather.redis.steam.annotation.AnnoRedisStreamListener;
import com.cjfather.redis.steam.annotation.config.RedisObjStringStreamConfig;
import com.cjfather.redis.steam.annotation.consumers.AnnoObjeStringConsumerI;

import lombok.extern.slf4j.Slf4j;

/**
 * @author: todd
 * @date:   2023年8月7日
 */


//@Configuration
@Slf4j
@Order(-1)
@Aspect
public class ConsumerAspect {
	
	@Resource
	RedisObjStringStreamConfig redisObjStringStreamConfig;
	
	//调用的方法有该注解才会拦截
    @Pointcut("execution(* com.cjfather..*.onMessage(..))")
    private void annotationPointCut() {
    }
 
 
   //环绕 满足上面的拦截条件就会调用该方法
    @Before("annotationPointCut()")
    public void annotationAround(JoinPoint joinPoint) throws Throwable {
    	AnnoObjeStringConsumerI consumer = (AnnoObjeStringConsumerI) joinPoint.getTarget();
    	
        //获取方法
        Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
 
        //得到方法参数  这里是一个数组，因为我这里只有一个参数所以写了[0]
        Object args = joinPoint.getArgs()[0];
       
 
        // 获取该方法上的注解
        AnnoRedisStreamListener aspectAnnotation = method.getAnnotation(AnnoRedisStreamListener.class);
 
        String key = aspectAnnotation.streamKey();
        redisObjStringStreamConfig.creasteSubscription(consumer, key, aspectAnnotation.consumerGroup(), aspectAnnotation.consumerName(), aspectAnnotation.consumerSize());
 
 
       //写你的业务逻辑
 
        log.info("todd:{} {}",key,args);
        
    }

        
}
